<div class="row">
    <div class="col-md-12">
        <div class="box-widget">
            <table id="list-group" class="table data-tbl-boxy responsive">
                <thead>
                    <tr>
                        <th>
                            <?php echo $this->translate('supplier name'); ?>
                        </th>
                        <!--<th>
                            <?php echo $this->translate('dept'); ?><br />
                            <span style="text-align: center;">(1)</span>
                        </th>-->
                        <th>
                            <?php echo $this->translate('Tổng mua'); ?><br />
                            <span style="text-align: center;">(1)</span>
                        </th>
                        <th>
                            <?php echo $this->translate('Tổng trả tiền'); ?><br />
                            <span style="text-align: center;">(2)</span>
                        </th>
                        <!--<th>
                            <?php echo $this->translate('refund'); ?><br />
                            <span style="text-align: center;">(4)</span>
                        </th>
                        <th>
                            <?php echo $this->translate('revenue'); ?><br />
                            <span style="text-align: center;">(5) = (2) - (4)</span>
                        </th>-->
                        <th>
                            <?php echo $this->translate('Còn nợ'); ?><br />
                            <span style="text-align: center;">(3) = (1) - (2)</span>
                        </th>
                        <th style="width: 10%;">
                            <?php echo $this->translate('Trả tiền thêm'); ?>
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                        $grandTotal = 0;
                        $grandDept = 0;
                        $grandTotalpay = 0;
                        $grandRemaining = 0;
                        $grandRefund = 0;
                        $grandRevenue = 0;
                    ?>
                    <?php foreach($this->dept as $key => $row): ?>
                    <?php
                        $dept = $row->dept;
                        $grandDept += $dept;
                        
                        $order = $this->orders[$key];
                        $total = $order->subtotal;
                        $grandTotal += $total;
                        
                        $payment = $this->payment[$key];
                        $totalpay = $order->totalpay + $payment->total;
                        $grandTotalpay += $totalpay;
                        
                        $refund = $this->refund[$key];
                        $remaining = $total - $totalpay + $dept - $refund->total;
                        $grandRemaining += $remaining;
                        $grandRefund += $refund->total;
                        
                        $revenue = $total - $refund->total;
                        $grandRevenue += $revenue;
                    ?>
                    <tr>
                        <td>
                            <a href="javascript:view('<?php echo $row->supplier_id; ?>')">
                                <?php echo $row->name; ?>
                            </a>
                        </td>
                        <!--<td>
                            <?php echo number_format($dept, 0, null, "."); ?>
                        </td>-->
                        <td>
                            <?php echo number_format($total, 0, null, "."); ?>
                        </td>
                        <td>
                            <?php echo number_format($totalpay, 0, null, "."); ?>
                        </td>
                        <!--<td>
                            <?php echo number_format($refund->total, 0, null, "."); ?>
                        </td>
                        <td>
                            <?php echo number_format($revenue, 0, null, "."); ?>
                        </td>-->
                        <td>
                            <?php echo number_format($remaining, 0, null, "."); ?>
                        </td>
                        <td>
                            <a title="<?php echo $this->translate('edit'); ?>" href="javascript:pay('<?php echo $row->supplier_id; ?>')" class="btn btn-mini btn-primary">
                                <?php echo $this->translate('checkout'); ?>
                            </a>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
                <tfoot>
                    <tr>
                        <td>
                            <strong>
                                <?php echo $this->translate('total'); ?>
                            </strong>
                        </td>
                        <!--<td>
                            <strong>
                                <?php echo number_format($grandDept, 0, null, "."); ?>
                            </strong>
                        </td>-->
                        <td>
                            <strong>
                                <?php echo number_format($grandTotal, 0, null, "."); ?>
                            </strong>
                        </td>
                        <td>
                            <strong>
                                <?php echo number_format($grandTotalpay, 0, null, "."); ?>
                            </strong>
                        </td>
                        <!--<td>
                            <strong>
                                <?php echo number_format($grandRefund, 0, null, "."); ?>
                            </strong>
                        </td>
                        <td>
                            <strong>
                                <?php echo number_format($grandRevenue, 0, null, "."); ?>
                            </strong>
                        </td>-->
                        <td colspan="2">
                            <strong>
                                <?php echo number_format($grandRemaining, 0, null, "."); ?>
                            </strong>
                        </td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
</div>
<script>
function pay(id){
    var totalpay = prompt("Nhập số tiền cần thanh toán", "0");
    totalpay = parseInt(totalpay);
    
    if(!isNaN(totalpay)){
        if(totalpay != 0){
            var form = '<form action="<?php echo $this->url(array('module' => 'admin', 'controller' => 'buy-liabilities', 'action' => 'payment'), null, true); ?>" method="post" id="edit-user-' + id + '">';
                form += '<input type="hidden" name="id" value="' + id + '" />';
                form += '<input type="hidden" name="total" value="' + totalpay + '" />';
                form += '</form>';
            $("table").append(form);
            $("#edit-user-" + id).submit();
        }
    }
}

function view(id){
    var form = '<form action="<?php echo $this->url(array('module' => 'admin', 'controller' => 'buy-liabilities', 'action' => 'view-detail'), null, true); ?>" method="post" id="edit-user-' + id + '">';
        form += '<input type="hidden" name="id" value="' + id + '" />';
        form += '</form>';
    $("table").append(form);
    $("#edit-user-" + id).submit();
}

$(function() {
    $('.data-tbl-boxy').dataTable({
        "sPaginationType": "bootstrap",
        "iDisplayLength": 25,
        "oLanguage": {
            "sLengthMenu": "<span class='lengthLabel pull-left'><?php echo $this->translate('entries_per_page'); ?>:</span><span class='lenghtMenu pull-left'> _MENU_</span>",
            "sInfo": "<?php echo $this->translate('show start end records of total'); ?>",
            "sSearch": "<?php echo $this->translate('filter'); ?>:",
            "oPaginate": {
                "sFirst": "<?php echo $this->translate('first page'); ?>",
                "sLast": "<?php echo $this->translate('last page'); ?>",
                "sNext": "<?php echo $this->translate('next page'); ?>",
                "sPrevious": "<?php echo $this->translate('previous page'); ?>"
            }
        },
        "sDom": '<"widget-head clearfix"fl>,<"widget-container"<"widget-block"<"widget-content"t>,,<"table-bottom clearfix"<"tbl-pagination pull-left"p><"tbl-data-info pull-right"i>>'
    });
});
</script>